package com.example.administrator.xiaoxiang.fragment.loginfragment;


import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.xiaoxiang.R;
import com.example.administrator.xiaoxiang.activity.MainActivity;
import com.example.administrator.xiaoxiang.fragment.PersonalFragment;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

import static com.bumptech.glide.gifdecoder.GifHeaderParser.TAG;

/**
 * A simple {@link Fragment} subclass.
 */
public class QuickLoginFragment extends Fragment
implements View.OnClickListener{
    private Button getCode;//获取验证码
    private Button login;
    private EditText phoneNumber;
    private EditText codeNumber;
    private EventHandler eh;
    private int count = 60;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x110) {
                count --;
                if (count<=0) {
                    getCode.setText("发送验证码");
                    getCode.setTextColor(Color.RED);
                    getCode.setEnabled(true);
                    count = 60;
                }else{
                    getCode.setText("重新发送（"+count+"）");
                    getCode.setTextColor(Color.GRAY);
                    getCode.setEnabled(false);
                    handler.sendEmptyMessageDelayed(0x110,1000);
                }
            }else if(msg.what == 0x120){
                getCode.setText("发送验证码");
                Toast.makeText(getContext(), "验证码不正确", Toast.LENGTH_SHORT).show();
                count = 60;
                getCode.setEnabled(true);
            }else if(msg.what == 0x130){
                Toast.makeText(getContext(), "登录成功", Toast.LENGTH_SHORT).show();
                MainActivity activity = (MainActivity) getActivity();
                activity.showFragment(PersonalFragment.TAG,PersonalFragment.class);
                activity.isLongin = true;
            }
        }
    };
    public QuickLoginFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
//        SMSSDK.initSDK(getContext(), "14e3dbf5f02f7", "30583cbc9f9a48ebd63a08bbe839215d");
        SMSSDK.initSDK(getContext(), "1ab9b2710cbf0", "2c48598fc5bea15649e6bee6029c2750");
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_quick_login, container, false);
        init(view);
        //回调完成
//提交验证码成功
//获取验证码成功
//返回支持发送验证码的国家列表
        eh = new EventHandler(){

            @Override
            public void afterEvent(int event, int result, Object data) {
                if (result == SMSSDK.RESULT_ERROR) {
                    handler.sendEmptyMessage(0x120);
                }
                if (result == SMSSDK.RESULT_COMPLETE) {
                    //回调完成
                    if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                        //提交验证码成功

                        handler.sendEmptyMessage(0x130);
                    }else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
                        //获取验证码成功
                        handler.sendEmptyMessage(0x110);
                    }else if (event ==SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){
                        //返回支持发送验证码的国家列表
                    }
                }else{
                    ((Throwable)data).printStackTrace();
                }
            }
        };
        SMSSDK.registerEventHandler(eh);
        return view;
    }

    private void init(View view) {
        getCode = (Button) view.findViewById(R.id.login_request_code_btn);
        login = (Button) view.findViewById(R.id.login_commit_btn);
        phoneNumber = (EditText) view.findViewById(R.id.login_input_phone_et);
        codeNumber = (EditText) view.findViewById(R.id.login_input_code_et);
        getCode.setOnClickListener(this);
        login.setOnClickListener(this);
        phoneNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().matches("^[1][34578]\\d{9}$")) {
                    getCode.setTextColor(Color.RED);
                    getCode.setEnabled(true);
                }else{
                    getCode.setTextColor(Color.GRAY);
                    getCode.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        String s =  phoneNumber.getText().toString();
        String code = codeNumber.getText().toString();
        switch (v.getId()){
            case R.id.login_request_code_btn:
                Log.e(TAG, "onClick: 点击了" );
                if (s.matches("^[1][34578]\\d{9}$")) {//初步验证是否是手机号码
                   SMSSDK.getVerificationCode("86",s);
                    Toast.makeText(getContext(), "正在生成验证码。。。", Toast.LENGTH_SHORT).show();
                    getCode.setText("请稍候。。");
                }else{
                    Toast.makeText( getContext(),"请输入正确的手机号码", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.login_commit_btn:
                Log.e(TAG, "onClick: "+code);
                if (s.matches("^[1][34578]\\d{9}$"))
                SMSSDK.submitVerificationCode("86", s,code);
                else
                    Toast.makeText(getContext(), "手机号与验证码不匹配", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        SMSSDK.unregisterEventHandler(eh);
    }
}
